package com.notebad.ui;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.notebad.R;
import com.notebad.note.Note;
import com.notebad.note.NotesDbManager;

public class AudioRecorderView extends Activity implements OnClickListener {

	private static final String TAG = "[AudioRecorderView]";
	private ImageButton recordButton;
	private ImageButton stopButton;

	private final MediaRecorder recorder = new MediaRecorder();
	private final String path= Note.getCurrentNote().getSoundsFolder() + "/" + "son-"+(Note.getCurrentNote().getMaxIndexSound() + 1) + ".3gp";


	public void onCreate(Bundle savedInstanceState) 
	{
		Log.d(TAG, "onCreate");
		super.onCreate(savedInstanceState);

		setContentView(R.layout.audio_recorder);		

		recordButton = (ImageButton) findViewById(R.id.Record);
		recordButton.setOnClickListener(this);

		stopButton = (ImageButton) findViewById(R.id.Stop);
		stopButton.setOnClickListener(this);
		stopButton.setEnabled(false);
	}

	protected void onDestroy() 
	{
		Log.d(TAG, "onDestroy");
		super.onDestroy();

		recorder.release();
	}

	@Override
	public void onClick(View view) 
	{
		switch(view.getId()) 
		{
		case R.id.Record:
			Log.d(TAG, "Record");
			try 
			{
				this.record();
			} 
			catch (IllegalStateException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
			return;

		case R.id.Stop:
			Log.d(TAG, "Stop");
			this.stop();
			return;
		}
	}

	/**
	 * Starts a new recording.
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	private void record() throws IllegalStateException, IOException
	{
		recorder.reset();
		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setOutputFile(path);
		recorder.prepare();
		recorder.start();
		stopButton.setEnabled(true);
		recordButton.setEnabled(false);
	}

	/**
	 * Stops a recording that has been previously started.
	 */
	private void stop()
	{
		recorder.stop();
		stopButton.setEnabled(false);
		recordButton.setEnabled(true);

		AlertDialog.Builder builder = new AlertDialog.Builder(AudioRecorderView.this);
		builder.setMessage("Enregistrer le son ?")
		.setCancelable(false)
		.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				//save sound

				if(Note.getCurrentNote()==null){
					Log.d(TAG,"Note courante nulle");
				}

				Note.getCurrentNote().incrementIndexSound();

				if(!Note.getCurrentNote().isNew()){
					NotesDbManager manager = new NotesDbManager(AudioRecorderView.this);
					manager.open();
					manager.updateNote(Note.getCurrentNote());
					manager.close();
				}
				Intent intent = new Intent(AudioRecorderView.this, VisualisationView.class);
				intent.putExtra("tab", 2);
				startActivity(intent);
				finish();
			}
		})
		.setNegativeButton("Non", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// Delete the file on the sdcard
				new File(path).delete();
				dialog.cancel();
				onResume();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
